package cloud.simple.ui.service;

import cloud.simple.ui.model.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 封装服务接口访问
 * Created by VIC on 2016/9/23.
 */
@Service
public class UserService {
    private final String SIMPLE_SERVICE = "simple-service";

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallbackSearchAll")   //方法失败后，系统将自动切换到fallbackMethod指定的方法执行
    public List<User> searchAll() {
        return restTemplate.getForObject("http://"+SIMPLE_SERVICE+"/users", List.class);
    }

    private List<User> fallbackSearchAll() {
        System.out.println("HystrixCommand fallbackMethod handle!");
        List<User> list = new ArrayList<User>(1);
        User user = new User();
        user.setUsername("TestHystrix");
        list.add(user);
        return list;
    }
}
